<?php
namespace Qwadmin\Controller;
class DomainController extends ComController{
    /**
     *  域名列表
     */
    public function index(){
        $map = [];
        $page = I("p" , 1, 'intval');
        if(isset($_GET['level'])){
            $map['level'] = I("level" , 0 , 'intval');
        }
        $data = D("Domain")->lists($map ,$page);
        $this->assign("data" ,$data );
        $this->display();
    }
    
    /**
     *  添加域名
     */
    public function add(){
        $this->display();
    }
    
    /**
     *  表单数据处理
     */
    public function hundle(){
        $content = htmlspecialchars(I("content") , ENT_NOQUOTES );
        $arr = explode("\n", $content);
        $level = I("level" , 0 , 'intval');
        $data = [] ;
        foreach($arr as $key => $value){
            if($value != ''){
                $data[] = [
                    'domain'    => $value,
                    'level'     => $level,
                    'status'    => 0
                ];
            }
        }
        
        $rows = D("Domain")->insertAll($data);
        
        if($rows >  0){
            $this->success("添加域名成功" , 'index');
        }else{
            $this->error("添加域名失败");
        }
    }
    
    /**
     *  更改域名状态
     */
    public function change_domain_status(){
        $id = I("id" , 0 , 'intval');
        $status = I("status" , 0 , 'intval');
        $domain = M("domain")->where(['id' => $id])->find();
        $current_level = $domain['level'];
        //将原来的启用域名设置为拦截
        M("domain")->where(['status' => 1 , 'level' =>$current_level])->save(['status' =>2]);
        $rows = M("domain")->where(['id' => $id])->save(['status' => $status]);
        if($rows){
            $this->ajaxReturn(['status' =>$rows , 'msg' =>'设置成功']);
        }
        $this->ajaxReturn(['status' =>0 , 'msg' =>'设置失败']);
    }
}
